package com.cloudpark.common.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class CommonUtil {

    public static String getRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getWxSign(Map<String, String> map, String key) {
        String str = createLinkString(map);
        System.out.println("stringA：" + str);
        String stringSignTemp = str + "&key=" + key;
        System.out.println("STRING B:" + stringSignTemp);
        String sign = MD5Util.getMd5ByStr(stringSignTemp).toUpperCase();
        System.out.println("result:" + sign);
        return sign;
    }

    public static String createLinkString(Map<String, String> params) {

        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        String prestr = "";

        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);

            if (i == keys.size() - 1) {// 拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }

        return prestr;
    }

    public static String maptoxmlNohead(Map<String, String> map) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<xml>");
        for (String key : map.keySet()) {
            buffer.append("<" + key + ">");
            // buffer.append("<![CDATA[");
            buffer.append(map.get(key));
            // buffer.append("]]>");
            buffer.append("</" + key + ">");
        }
        buffer.append("</xml>");

        String xmlString = "";
        try {
            xmlString = new String(buffer.toString().getBytes("UTF-8"));
            // xmlString = new String(buffer.toString().getBytes(),
            // "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return xmlString;
    }

    public static String maptoxml(Map<String, String> map) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<xml>");
        for (String key : map.keySet()) {
            buffer.append("<" + key + ">");
             buffer.append("<![CDATA[");
            buffer.append(map.get(key));
             buffer.append("]]>");
            buffer.append("</" + key + ">");
        }
        buffer.append("</xml>");
        return buffer.toString();
    }

    /**
     * 将字符串转成unicode
     * 
     * @param str
     *            待转字符串
     * @return unicode字符串
     */
    public static String convert(String str) {
        str = (str == null ? "" : str);
        String tmp;
        StringBuffer sb = new StringBuffer(1000);
        char c;
        int i, j;
        sb.setLength(0);
        for (i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            sb.append("\\u");
            j = (c >>> 8); // 取出高8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);
            j = (c & 0xFF); // 取出低8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);

        }
        return (new String(sb));
    }

    /**
     * 将unicode 字符串
     * 
     * @param str
     *            待转字符串
     * @return 普通字符串
     */
    public static String revert(String str) {
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;

        while ((i = str.indexOf("\\u", pos)) != -1) {
            sb.append(str.substring(pos, i));
            if (i + 5 < str.length()) {
                pos = i + 6;
                sb.append((char) Integer.parseInt(str.substring(i + 2, i + 6), 16));
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(convert("sd"));
        System.out.println(revert("\u5927\u767d"));
        System.out.println(getRandomString(32));
    }
}
